package com.yzt.dynamicmodule.adapter;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.yzt.dynamicmodule.R;

import com.yzt.zhuju.network.entity.dynamic.DynamicMsgBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicMsgChildBean;
import com.yzt.zhuju.widget.DisableRecyclerView;
import com.yzt.plugin_imageloader.ImageLoadUtil;

/**
 * author : bob
 * date   : 2023/2/28
 * desc   : 动态模块留言消息列表
 */
public class DynamicMsgAdapter extends BaseQuickAdapter<DynamicMsgBean, BaseViewHolder> {

    public DynamicMsgAdapter() {
        super(R.layout.item_dynamic_msg);
    }

    @Override
    protected void convert(BaseViewHolder holder, DynamicMsgBean item) {

        String head = item.getAvatar();
        ImageLoadUtil.loadAvatar(getContext(), head, holder.getView(R.id.img_header));
        holder.setText(R.id.tv_user_name, item.getNickName());
        holder.setText(R.id.tv_time, item.getCreateTime());

        holder.setText(R.id.tv_content, item.getContent());

        DisableRecyclerView recyclerView = holder.findView(R.id.dis_recycler_view);
        recyclerView.setVisibility(View.GONE);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        DynamicMsgChildAdapter adapter = new DynamicMsgChildAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setList(item.getCommentList());
        adapter.addChildClickViewIds(R.id.tv_like, R.id.tv_reply);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter1, @NonNull View view, int position) {
                DynamicMsgChildBean bean = adapter.getItem(position);
                if (view.getId() == R.id.tv_like) {
                    if (listener != null) {
                        listener.childLikeClick(bean, item);
                    }
                }
                if (view.getId() == R.id.tv_reply) {
                    if (listener != null) {
                        listener.childItemClick(bean, item);
                    }
                }
            }
        });

        if (item.getCommentList().isEmpty()) {
            holder.setVisible(R.id.tv_num_replies, false);
        } else {
            holder.setVisible(R.id.tv_num_replies, true);
            if (item.isShowAll()) {
                holder.setText(R.id.tv_num_replies, "收起");
                recyclerView.setVisibility(View.VISIBLE);
            } else {
                holder.setText(R.id.tv_num_replies, "展开");
                recyclerView.setVisibility(View.GONE);
            }
        }
    }

    public DynamicMsgClickListener listener;

    public void setDynamicMsgClickListener(DynamicMsgClickListener dynamicMsgClickListener) {
        if (listener == null) {
            listener = dynamicMsgClickListener;
        }
    }

    public interface DynamicMsgClickListener {

        void childItemClick(DynamicMsgChildBean bean, DynamicMsgBean item);

        void childLikeClick(DynamicMsgChildBean bean, DynamicMsgBean item);
    }
}
